package cc.lishixi.book.controller;

import cc.lishixi.book.entity.Librarian;
import cc.lishixi.book.service.LibrarianService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by LiShixi on 2016/7/24 0024.
 * http://www.lishixi.cc
 */
@Controller
@RequestMapping("librarian")
public class LibrarianController {
    @Autowired
    LibrarianService librarianService;

    @RequestMapping("login")
    @ResponseBody
    public String login(Librarian librarian,@RequestParam Integer userType) {
        if (librarian.getUserid() != null) {
            Librarian lib = librarianService.get(librarian.getUserid());
            if(lib.getUserid()==null){
                return "用户不存在";
            }

            boolean isAdmin = (lib.getBookp() == 1);
            boolean isReader = (lib.getReaderp() == 1);
            if(!(isAdmin||isReader)){
                return "权限不足！";
            }
            if (!librarian.getPasswd().equals(lib.getPasswd())) {
                return "用户名密码错误";
            }

            boolean selAdmin = (userType == 0);
            boolean selReader = (userType == 1);
            if(selAdmin){
                return "librarian/index.do";
            }else if(selReader){
                return "book/list.do";
            }
        } else {
            return "请输入用户名";
        }
        return "true";
    }

    @RequestMapping("index")
    public String index() {
        return "librarian/index";
    }

    @RequestMapping("find")
    public String find(){
        return "book/find";
    }

    @RequestMapping("borrow")
    public String borrow(){
        return "book/borrow";
    }

    @RequestMapping("sys")
    public String sys(){
        return "librarian/sys";
    }
}
